﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using xIx.Web.UI;

public partial class DataItemEditor : PageBase
{
    public string BizID
    {
        get
        {
            object v = ViewState["BizID"];
            if (null != v)
            {
                return (string)v;
            }
            else
            {
                return default(string);
            }
        }
        set
        {
            ViewState["BizID"] = value;
        }
    }
    public string Gid
    {
        get
        {
            object v = ViewState["Gid"];
            if (null != v)
            {
                return (string)v;
            }
            else
            {
                return default(string);
            }
        }
        set
        {
            ViewState["Gid"] = value;
        }
    }
    public string GPath
    {
        get
        {
            object v = ViewState["GPath"];
            if (null != v)
            {
                return (string)v;
            }
            else
            {
                return default(string);
            }
        }
        set
        {
            ViewState["GPath"] = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BizID=Request["ID"];
            Gid = Request["Gid"];
            TimeControl1.Value = DateTime.Now;
            if (string.IsNullOrEmpty(BizID)&&string.IsNullOrEmpty(Gid)) {
                SetPageDisable();
                return;
            }

            DataBind();
        }
    }
    protected void ResetButton_Click(object sender, ImageClickEventArgs e)
    {
        DataBind();
    }

    public string UseUserImgSelCtl()
    {
        UserImgSelector1.SaveFile();
        return UserImgSelector1.Text;
        //return ImgSel1.Value;
    }

    protected void SaveButton_Click(object sender, ImageClickEventArgs e)
    {
        if (string.IsNullOrEmpty(Gid)) { SetPageDisable(); return; }
        int erc = 0;
        using (xIx.Data.DbHelper dh = new xIx.Data.DbHelper())
        {
            DataTable dt = dh.Table["Sys_Groups"].Fields("ID,Path,IsItemHasSubGroup,ItemSubGroupSet").Where("ID=@RID").Select(Gid);
            if (dt.Rows.Count == 0) { SetPageDisable("类别提取失败。"); return; }
            GPath = dt.Rows[0][1].ToString();

            double vs = 10; double.TryParse(TextBox3.Text, out vs);

            if (string.IsNullOrEmpty(BizID))
            {
                string nid = dh.GenUid().ToString();
                erc = dh.Table["Sys_ListItems"]
                    .Fields("ID,Title,Gid,SortIdx,Img,Text,ContentTime,Editor,UpdateTime,IsAudit,GPath,AttachFile,Tag")
                    .InsertWith(nid, TextBox2.Text, Gid, vs, UserImgSelector1.Value, FCKeditor1.Value, TimeControl1.Value, TextBox6.Text, DateTime.Now, (User.AccountInfo.Role == "Admin" || User.AccountInfo.Role == "Manager"), GPath + "/"
                    ,UseUserImgSelCtl(),TextBox4.Text)
                    .Exec();
                if (erc == 1) 
                {
                    BizID = nid;

                    if (erc == 1) 
                    {
                        if (dt.Rows[0]["IsItemHasSubGroup"].ToString()=="1")
                        {
                            int sortidx = 100;
                            foreach (string sgtitle in dt.Rows[0]["ItemSubGroupSet"].ToString().Split(','))
	                        {
                                string ngid=dh.GenUid().ToString();
		                        dh.Table["Sys_Groups"].Fields("ID,Title,Pid,Path,SortIdx")
                                    .InsertWith(ngid,sgtitle,BizID,GPath+BizID+"/"+ngid,sortidx--).Exec();
	                        } 
                        }
                    }
                }
            }
            else
            {
                erc = dh.Table["Sys_ListItems"]
                    .Fields("Title,SortIdx,Img,Text,ContentTime,Editor,UpdateTime,IsAudit,AttachFile,Tag".Split(','))
                    .UpdateWith(TextBox2.Text, vs, UserImgSelector1.Value, FCKeditor1.Value, TimeControl1.Value, TextBox6.Text, DateTime.Now, CheckBox1.Checked && CanAudit
                    , UseUserImgSelCtl(), TextBox4.Text)
                    .Where("ID=@RID")
                    .Exec(BizID);
            }


        }


        if (erc == 1)
        {
            DataBind();
        }
        else
        {
            
            //UserImgSelector1.DelFile();
            Alert("保存失败。");
        }

    }
    
    protected override void OnDataBinding(EventArgs e)
    {
        if (string.IsNullOrEmpty(BizID)) {
            if(string.IsNullOrEmpty(Gid)){SetPageDisable();return;}
            using (xIx.Data.DbHelper dh = new xIx.Data.DbHelper())
            {
                DataTable dt= dh.Table["Sys_Groups"].Fields("ID,Path").Where("ID=@RID").Select(Gid);
                if(dt.Rows.Count==0){SetPageDisable("类别提取失败。");return;}
                GPath = dt.Rows[0][1].ToString();
            }

            TextBox1.Text = TextBox2.Text = TextBox3.Text = TextBox4.Text = UserImgSelector1.Value = TextBox6.Text = FCKeditor1.Value = string.Empty;
            TimeControl1.Value = DateTime.Now;
            TextBox6.Text = User.AccountInfo.Title;
            CheckBox1.Enabled = false;
        }
        else
        {
            DataTable dt=null;
            using (xIx.Data.DbHelper dh = new xIx.Data.DbHelper())
            {
                dt = dh.Table["Sys_ListItems"].Where("ID=@RID").Select(BizID);
            }
            if (dt.Rows.Count == 0) { SetPageDisable("没有数据。"); return; }

            Gid = dt.Rows[0]["Gid"].ToString();
            GPath = dt.Rows[0]["GPath"].ToString();

            TextBox1.Text = dt.Rows[0]["ID"].ToString();
            TextBox2.Text = dt.Rows[0]["Title"].ToString();
            TextBox3.Text = dt.Rows[0]["SortIdx"].ToString();
            TextBox4.Text=dt.Rows[0]["Tag"].ToString();
            //ImgSel1.Value = dt.Rows[0]["Img"].ToString();
            UserImgSelector1.Value = dt.Rows[0]["AttachFile"].ToString();
            try
            {
                TimeControl1.Value = (DateTime)dt.Rows[0]["ContentTime"];
            }
            catch { }
            TextBox6.Text = dt.Rows[0]["Editor"].ToString();
            FCKeditor1.Value = dt.Rows[0]["Text"].ToString();
            CheckBox1.Checked=(bool)dt.Rows[0]["IsAudit"];
            CheckBox1.Enabled = CanAudit;
        }
        base.OnDataBinding(e);
    }
    public bool CanAudit { 
        get{return (User.AccountInfo.Role == "Manager" || User.AccountInfo.Role == "Admin");}
    }

}
